package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * http://www.0731fdc.com/ 0731房产网
 *  abc
 */
public class _0731fdcNewsAnalyse implements AnalyseNews{
	
	public boolean isDetailPage(String url) {
		boolean bRet=false;
		String[] regex={
				"http://.*.0731fdc.com/.*/[0-9]*/[0-9]{2}/[0-9]*_1.html",
				"http://.*.0731fdc.com/.*/[0-9]*/[0-9]{2}/.*_[0-9]*_1.html",
				"http://.*.0731fdc.com/html/[0-9]{4}/[a-z]*_[0-9]*/[0-9]*.html",
				"http://news.0731fdc.com/fc/[0-9]{6}/[0-9]{2}/_[0-9]*.html"
				};
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();
		if(!isDetailPage(url)){
		}
		news.setUrl(url);

		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);

		title =doc.select(".title").text();
		if(StringUtils.isBlank(title)){
			title =doc.select("div#Article h1").text();
		}
		date=DateUtils.matchDate(doc.select(".artinfo").text());
		if(date==null){
			date=DateUtils.matchDate(doc.select("div#name").text());
			if(date == null){
				date=DateUtils.matchDate(doc.select("div#Article span").text());
			}
		}
		content=HtmlCleaner.getContentHtml(url,doc.select(".artibody"));
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("div#news_content"));
		}
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("div.content"));
		}

		/*
		 * 作者
		 */
		String authorStr=null;
		authorStr=doc.select("div.artinfo").text();
		if(StringUtils.isBlank(authorStr)){
			authorStr=doc.select("div#Article").text();
			author=StringUtils.substringBetween(authorStr, "来源：", "评论：");
			if(StringUtils.isBlank(author)){
				author=StringUtils.substringBetween(authorStr, "来源：", " ");
			}
		}

		if(StringUtils.isBlank(author)){
			author=StringUtils.substringAfter(authorStr, " ");
			author=StringUtils.substringAfter(author, " ");
		}

		if(StringUtils.isBlank(title)){
		}
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setAuthor(author);
		news.setDate(date);
		/**
		 * 解析用于更新的地址
		 */
		String updateUrl=null;

		return news;
	}
	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}
	public static void main(String[] args) {
		_0731fdcNewsAnalyse a=new _0731fdcNewsAnalyse();
		String url="http://news.0731fdc.com/fc/201611/04/16264235_1.html";
		String fromatUrl=UrlArgumentTop.FromatUrl(url);
		System.out.println("url="+fromatUrl);
		System.out.println(a.isDetailPage(fromatUrl));
		UrlMeta meta=CrawlHTML.responseToURL(fromatUrl);
		NewsMeta parserHtml=a.parserHtml(meta);
		System.out.println(parserHtml);
	}
	
	public boolean isNeedUpdate(){
		return false;
	}
}
